import 'dart:async';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/bank_controller/bank_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:common_utils/common_utils.dart';
import 'package:xinxuan/vo/verification_vo.dart';

class PayBankDetailPageController extends GetxController {

  final BankPageController bankPageController = Get.find();
  final CommonPageController commonPageController = Get.find();

  /// 剩余秒数
  var duration = 60.obs;
  /// 时间倒计时
  Timer? _timer;
  /// 验证码
  String verificationCode = '';
  String verificationKey = '';

  /// 获取验证码
  getSmsCode() async {
    VerificationVo? res = await HttpUtilService.getVerificationCode(mobile: commonPageController.userData.value.mobile!, type: 'update_bank');
    if (res == null) {
      return;
    }
    verificationKey = res.data!.verificationKey!;
    EasyLoading.showToast('发送成功');
    _timer?.cancel();
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      duration.value--;
      print('duration.value: ${duration.value }');
      if (duration.value == 0) {
        _timer?.cancel();
        duration.value = 60;
      }
    });
  }

  /// 确定解绑
  unboundAddPayBank(int paymentId) async {
    if (TextUtil.isEmpty(verificationCode)) {
      EasyLoading.showToast('请输入验证码');
      return;
    }
    var res = await HttpUtilService.unboundAddPayBank(paymentId: paymentId, verificationCode: verificationCode, verificationKey: verificationKey);
    if (!res) {
      return;
    }
    Get.back();
    Get.back();
    EasyLoading.showToast('解绑成功');
    bankPageController.getBankCardList();
  }

  @override
  void onClose() {
    _timer?.cancel();
    super.onClose();
  }
  
}